package com.ujson.chat.client.builder;

import com.ujson.base.common.utils.StringUtil;
import com.ujson.chat.common.dto.UserDto;
import com.ujson.chat.common.proto.MessageProto;
import com.ujson.chat.common.utils.Constants;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * <h3>ujson-parent</h3>
 * <p>登录消息创建器</p>
 *
 * @author JerryLiu  a1031757312@gmail.com
 * @version 1.0
 * @date 2021-11-23 21:48
 */
@Slf4j
@Service
public class LoginMessageBuilder extends AbstractBuilder {

    @Override
    public MessageProto.MessageType getType() {
        return MessageProto.MessageType.LOGIN_REQUEST;
    }

    public MessageProto.Message buildLoginMessage(Channel channel, UserDto userDto) {
        MessageProto.LoginRequest loginRequest = MessageProto.LoginRequest.newBuilder()
                .setUsername(userDto.getUsername())
                .setPassword(userDto.getPassword())
                .setUnixTime(System.currentTimeMillis())
                .setDeviceId(StringUtil.genUUID())
                .setPlatform(MessageProto.Platform.WINDOWS.getNumber())
                .setAppVersion(Constants.APP_VERSION)
                .build();
        return commonBuild(channel).toBuilder()
                .setLoginRequest(loginRequest)
                .build();
    }

}
